package com.itheima.shoppingmall;

import com.itheima.shoppingmall.pay.Alipay;
import com.itheima.shoppingmall.pay.PayInterface;
import com.itheima.shoppingmall.pay.PaySystem;
import com.itheima.shoppingmall.pay.WeChatPay;
import com.itheima.shoppingmall.user.Business;
import com.itheima.shoppingmall.user.Member;
import com.itheima.shoppingmall.user.User;

import javax.sql.rowset.serial.SerialStruct;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

/**
 * 程序入口类
 */
public class MainApp {
    public static void main(String[] args) {
        //创建用户集合
        ArrayList<User> usersList = new ArrayList<>();
        usersList.add(new Business("波仔_user", "618719"));//商户
        usersList.add(new Member("黑马_user", "618719"));//会员

        //创建订单集合
        ArrayList<Order> ordersList = new ArrayList<>();

        //创建商品集合
        ArrayList<Goods> goodsList = new ArrayList<>();
        goodsList.add(new Goods(goodId(), "苹果手机", 8000.0, "手机"));
        goodsList.add(new Goods(goodId(), "HP笔记本", 6000.0, "电脑"));
        goodsList.add(new Goods(goodId(), "罗技鼠标", 100.0, "鼠标"));
        goodsList.add(new Goods(goodId(), "小米手机", 0000.0, "手机"));

        Scanner scanner = new Scanner(System.in);
        System.out.println("---欢迎进入黑马商城---");
        System.out.println("请输入用户名");
        String id = scanner.next();
        System.out.println("请输入用户密码");
        String password = scanner.next();
        User user1 = checkLogin(id, password, usersList);
        for (User user : usersList) {
            if (user instanceof Business) {
               if (user.getLoginName().equals(id)==true){
                  if (user.getPassword().equals(password)){
                       //商户登录
                       showBusinessUI(user1, goodsList);
                   }else {
                      System.out.println("密码错误");
                  }

               }

            }
            
            if (user1 instanceof Member) {
                if (user.getLoginName().equals(id)==true){
                    if (user.getPassword().equals(password)){
                        //会员登录
                        showMemberUI(user1, goodsList, ordersList);
                    }else {
                        System.out.println("cuo");
                    }

                }else {
                    System.out.println("cuo");
                }

            }
        }



    }//main




    /**
     * 浏览商品
     *
     * @param scanner
     * @param goodsList
     * @param ordersList
     */
    public static void browseGoodsList(Scanner scanner, ArrayList<Goods> goodsList, ArrayList<Order> ordersList) {

    }

    /**
     * 用户登录
     *
     * @param id
     * @param password
     * @param usersList
     * @return
     */
    public static User checkLogin(String id, String password, ArrayList<User> usersList) {
        User user = new User();
        user.setLoginName(id);
        user.setPassword(password);

        return user;
    }

    /**
     * 查看商品
     *
     * @param goodsList
     */
    public static void findAllGoods(ArrayList<Goods> goodsList) {
        for (Goods goods : goodsList) {
            System.out.println(goods.toString());
        }

    }

    /**
     * 查看订单
     *
     * @param ordersLIst
     */
    public static void findAllOrder(ArrayList<Order> ordersLIst) {
        for (Order order : ordersLIst) {
            System.out.println(order.toString());
        }
    }

    /**
     * 上架商品
     *
     * @param scanner
     * @param goodsList
     */
    public static void goodsUpperShelf(Scanner scanner, ArrayList<Goods> goodsList) {
        Goods goods = new Goods();
        goods.setId(goodId());

        System.out.println("请输入商品名称");
        goods.setName(scanner.next());

        System.out.println("请输入商品单间");
        goods.setPrice(scanner.nextDouble());

        System.out.println("请输入商品类别");
        goods.setCategory(scanner.next());
        goodsList.add(goods);
    }

    //生成商品编号
    public static String goodId() {
        Random random = new Random();
        String id = "";
        String str = "ASDFGHJKLZXCVBNMQWERTYUIOP";
        id += str.charAt(random.nextInt(26));
        for (int i = 0; i < 4; i++) {
            int j = random.nextInt(9);
            id += j;
        }
        return id;
    }

    /**
     * 下架商品
     *
     * @param scanner
     * @param goodsList
     */
    public static void goodsDownShelf(Scanner scanner, ArrayList<Goods> goodsList) {

        System.out.println("请输入要下架商品的编号");
        String id = scanner.next();
        for (int i = 0; i < goodsList.size(); i++) {
            if (goodsList.get(i).getId().equals(id) == true) {
                Goods remove = goodsList.remove(i);
                System.out.println("已成功删除" + remove.getName());
            }
        }

    }

    /**
     * 进入商户界面
     *
     * @param user
     * @param goodsList
     */
    public static void showBusinessUI(User user, ArrayList<Goods> goodsList) {
        while (true) {

            System.out.println("---欢迎" + user.getLoginName() + "进入商城---");
            System.out.println("1. 上架商品\n" + "2. 下架商品\n" +
                    "3. 查看商品\n" + "4. 商户退出");
            System.out.println("-----------");
            System.out.println("请选择功能");
            Scanner scanner = new Scanner(System.in);
            int num = scanner.nextInt();
            switch (num) {
                case 1:
                    goodsUpperShelf(scanner, goodsList);
                    break;
                case 2:
                    goodsDownShelf(scanner, goodsList);
                    break;
                case 3:
                    findAllGoods(goodsList);
                    break;
                case 4:
                    return;
                default:
                    break;
            }

        }//

    }

    /**
     * 进入会员界面
     *
     * @param user
     * @param goodsList
     * @param ordersList
     */
    public static void showMemberUI(User user, ArrayList<Goods> goodsList, ArrayList<Order> ordersList) {
        while (true) {
            System.out.println("-----欢迎" + user.getLoginName() + "进入黑马商城-----");
            System.out.println("1.浏览商品\n" + "2.查看订单\n" + "3.会员退出");
            Scanner scanner = new Scanner(System.in);
            int num = scanner.nextInt();

            switch (num) {
                case 1:
                    browseGoodsList(scanner, goodsList, ordersList);
                    break;
                case 2:
                    findAllOrder(ordersList);
                    break;
                case 3:
                    return;
                default:
                    System.out.println("请输入正确的功能码");
                    break;
            }
        }
    }
}
